<?php

/** Cms_Resource_CreditPurchase_Item */
if (!class_exists('Cms_Resource_CreditPurchase_Item')) {
    require_once dirname(__FILE__) . '/CreditPurchase/Item.php';
}

/**
 * Cms_Resource_CreditPurchase
 *
 * @category   Cms
 * @package    Cms_Model_Resource
 */
class Cms_Resource_CreditPurchase extends BIS_Model_Resource_Db_Table_Abstract implements Cms_Resource_CreditPurchase_Interface
{
    protected $_name    = 'creditpurchase';
    protected $_primary  = 'entryId';
    protected $_rowClass = 'Cms_Resource_CreditPurchase_Item';

    /**
     * Get CreditPurchase by its entryId
     *
     * @param int $id The id to search for
     * @return Cms_Resource_CreditPurchase_Item|null
     */
    public function getCreditPurchaseByEntryId($id)
    {
        return $this->find($id)->current();
    }

    /**
     * Get list of credit purchases
     *
     * @param array      $data order, page, limit, search
     * @return Zend_Db_Table_Rowset|Zend_Paginator
     */
    public function getCreditPurchases($data= array())
    {
        $select = $this->select();

        $select->from(array('i'=>'creditpurchase'),
                        array('i.*',
                        	  'product' => 'p.name',
                        	  'supplier' => 's.name'
                            ))
                ->setIntegrityCheck(false)
                ->join(array('p'=>'product'), 'p.productId = i.productId', array('p.name'))
        		->join(array('s'=>'supplier'), 's.supplierId = i.supplierId', array('s.name'));

        if (isset($data['search'])) {
            $search = $data['search'];
            $select->where("p.name like ? OR s.name like ?", "%$search%");
        }
        
        if (isset($data['order'])) {
            $select->order($data['order']);
        }

	if (isset($data['page'])) {
		$adapter = new Zend_Paginator_Adapter_DbTableSelect($select);
		$paginator = new Zend_Paginator($adapter);
		$paginator->setItemCountPerPage($data['limit'])
	          	  ->setCurrentPageNumber((int) $data['page']);
		return $paginator;
	}

        return $this->fetchAll($select);
    }

    /**
     * Get Total Credit Purchase By Product Id
     *
     * @param int      $productId Product Id
     * @return int     Total credit purchase for the product
     */
    public function getTotalCreditPurchaseByProductId($productId)
    {
        $select = $this->select()
                ->from('creditpurchase',array('SUM(quantity) AS total'))
                ->where("productId = ?", $productId);
        
        $row = $this->fetchRow($select);

        return $row->total;
    }

    /**
     * Get Total Credit Purchase By Supplier Id
     *
     * @param int      $supplierId Supplier Id
     * @return int     Total credit purchase for the Supplier
     */
    public function getTotalCreditPurchaseBySupplierId($supplierId)
    {
        $select = $this->select()
                ->from('creditpurchase',array('COUNT(supplierId) AS total'))
                ->where('supplierId = ?', $supplierId);

        $row = $this->fetchRow($select);

        return $row->total;
    }

}